//
//  JZHCityListViewController.m
//  极致荟
//
//  Created by hetao on 14-5-19.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHCityListViewController.h"
#import "JZHActivityViewController.h"
@interface JZHCityListViewController ()
{
    NSDictionary * citylist;//城市列表
    NSDictionary *cityarr;//热门城市数组
    NSMutableArray *socityarr;//搜索结果数据
}
@property (weak, nonatomic) IBOutlet UILabel *topCityName;

@end

@implementation JZHCityListViewController

- (id)initController:(UIViewController *)controller
{
    self=[[JZHCityListViewController alloc]init];
    self.controller=controller;
    if(self)
    {
        return self;
    }else
    {
        return nil;
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    //设置左侧按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.navigationItem.title=@"城市选择";
    [self.citytext setReturnKeyType:UIReturnKeySearch];
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    [self addcityName:nil];
    socityarr=[[NSMutableArray alloc]init];
}

- (void)leftDrawerButtonPress:(UIButton*)but
{
    [UINavigationController Pop:self];
}

- (void)addcityName:(NSString *)str
{
    NSString *citypath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    citypath=[citypath stringByAppendingString:@"/CityList.plist"];
    citylist=[[NSDictionary alloc]initWithContentsOfFile:citypath];
    if(str)
    {
        NSDictionary *dic=[citylist objectForKey:@"City"];
        NSArray *arr=[dic allValues];
        if(!socityarr)
        {
            socityarr=[[NSMutableArray alloc]init];
        }
        for (NSString *cityname in arr)
        {
            NSRange range=[cityname rangeOfString:str];
            
            if(range.length>0)
            {
                
                [socityarr addObject:cityname];
                
            }
        }
        
        [self.table reloadData];
        return;
    }
    
    
    cityarr=[citylist objectForKey:@"City"];
    socityarr=[cityarr allValues];
    NSString *citystr=[[citylist objectForKey:@"Dcity"] objectForKey:@"name"];
    if(citystr.length<1)
    {
        self.topCityName.text=@"暂时无法获取当前城市";
    }else
    {
        self.topCityName.text=[[citylist objectForKey:@"Dcity"] objectForKey:@"name"];
        
    }
    [self.table reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark-------TableView代理----------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    return  socityarr.count+1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0)
    {
        return self.topcell.frame.size.height;
    }
    return 40;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *pengyoucell=@"PengyouCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:pengyoucell];
    if(indexPath.row==0)
    {
        return self.topcell;
    }
    if(!cell)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pengyoucell];
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 38, 320, 2)];
        [lab setBackgroundColor: [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1]];
        [cell.contentView addSubview:lab];
        [cell.contentView setBackgroundColor: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]];
        
        [cell.textLabel setFont:[UIFont systemFontOfSize:14.0]];
        [cell.textLabel setTextColor:[UIColor whiteColor]];
        
    }
    
    if(socityarr.count==0)
    {
        cell.textLabel.text=[cityarr allValues][indexPath.row-1];
        
    }else
    {
        cell.textLabel.text=socityarr[indexPath.row-1];
        
    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.controller && [self.controller isKindOfClass:[JZHActivityViewController class]])
    {
         NSString *cityname=[tableView cellForRowAtIndexPath:indexPath].textLabel.text;
        [(JZHActivityViewController *)self.controller partycityId:[NSNumber numberWithInt:[[cityarr allKeysForObject:cityname][0] intValue]]];
       
        [UINavigationController Pop:self];
    }
    if(indexPath.row!=0)
    {
        
        NSString *cityname=[tableView cellForRowAtIndexPath:indexPath].textLabel.text;
        NSLog(@"%@",[cityarr allKeysForObject:cityname][0]);
        [User defuceUser].citydic=[[NSDictionary alloc]initWithObjectsAndKeys:cityname,@"name",[cityarr allKeysForObject:cityname][0],@"id", nil];
        [UINavigationController Pop:self];
    }
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if([textField.text isEqualToString:@""])
    {
        [self addcityName:nil];
    }else
    {
        socityarr=nil;
        [self addcityName:textField.text];
    }
    
    return YES;
}


@end
